﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using edowork.com.SQL2005;
using edowork.com.Model;

namespace WebInvoicingsys.system.baseset.sysdata
{
    public partial class area_add : System.Web.UI.Page
    {
        //前台输出提示信息变量
        public string alert_msg = "";

        private void Reset()
        {
            uc_from_warehouse.Reset();
            txt_areaid.Text = "";
            txt_areaname.Text = "";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ;
            }
        }

        //保存
        protected void btnSave_Click(object sender, EventArgs e)
        {
            alert_msg = "";

            edowork.com.Model.areaInfo areaInfo = new areaInfo();

            areaInfo.Warehouseid = uc_from_warehouse.getValue();
            areaInfo.Areaid = txt_areaid.Text.Trim();
            areaInfo.Areaname = txt_areaname.Text.Trim();
            areaInfo.Sysdate = DateTime.Now.ToString("yyyy-MM-dd");


            if (String.IsNullOrEmpty(areaInfo.Warehouseid))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请选择所在仓库");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            if (String.IsNullOrEmpty(areaInfo.Areaid))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请输入库区编码");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                txt_areaid.Focus();

                return;
            }

            if (String.IsNullOrEmpty(areaInfo.Areaname))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请输入库区名称");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                txt_areaname.Focus();

                return;
            }

           
            //保存数据入数据库
            using (edowork.com.SQL2005.TranUtil tranutil = new TranUtil(Session["connectionString"].ToString()))
            {
                System.Data.SqlClient.SqlTransaction tran = null;

                try
                {
                    tran = tranutil.GetSqlTransaction();
                }
                catch (Exception er)
                {
                    alert_msg = String.Format(Scripts.alert_warning, er.Message);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                edowork.com.SQL2005.Execute exec = new Execute(Session["connectionString"].ToString());

                string sql = "";
                int result = -1;

                //保存入area表
                sql = String.Format(areaInfo.SQL_Insert(), areaInfo.Warehouseid, areaInfo.Areaid, areaInfo.Areaname, areaInfo.Sysdate);

                result = exec.ExecSQLQueryInt(sql, ref alert_msg, tran);

                if (result <= 0)
                {
                    tran.Rollback();
                    tranutil.CloseConnection();

                    alert_msg = String.Format(Scripts.alert_warning, alert_msg);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                tran.Commit();
                tranutil.CloseConnection();
            }

            Reset();

            alert_msg = String.Format(Scripts.alert_success, Errormsg.success_Save);

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
        }

        //重置
        protected void btnReset_Click(object sender, EventArgs e)
        {
            Reset();
        }

        //返回
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("/system/baseset/sysdata/area.aspx");
        }
    }
}